<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Administrator
 */
class ReceivingOrderList extends WMS_Controller
{

    var $whsCode;

    function __construct()
    {
        parent::__construct();
        $gp = gp();
        $userData = $this->session->userdata('users');
        $this->whsCode = $userData['user_main_whs_code'];
        //加载模型
        $this->load->service('wms/receiving_order_service');
        $this->load->service('wms/api_service');
        $this->load->service('wms/grid_service');
        $this->load->service('wms/warehouse_list_service');
        $this->load->service('wms/sap_service');
    }

    public function show_list()
    {
        $gp = gp();
        $refNum = $gp['refNum'] ? $gp['refNum'] : '';
        $sku = $gp['sku'] ? $gp['sku'] : '';
        $page = $gp['page'] ? intval($gp['page']) : 1;
        //锁死仓库
        $whs = $this->whsCode;
        $str = '?sku=' . $sku . '&refNum=' . $refNum;
        //分页查询
        $this->load->library('pagination');
        $config['page_query_string'] = TRUE;
        $config['base_url'] = blang_url('wms/ReceivingOrderList/show_list' . $str);
        $config['total_rows'] = $this->receiving_order_service->get_receiving_order_list_total($whs, $sku, $refNum);
        $config['uri_segment'] = config_item('enable_lang') ? 5 : 4;
        $config['per_page'] = 20;
        $this->pagination->initialize($config);
        $data['pageShow'] = $this->pagination->create_links();
        //分页结束
        //获取产品库存列表
        $receivingList = $this->receiving_order_service->get_receiving_order_list($whs, $sku, $refNum, $config['per_page'], ($page - 1) * $config['per_page']);
        //获取产品名称
        $receivingList = $this->warehouse_list_service->fields_whs_code($receivingList);
        $data['receivingList'] = $this->api_service->fields_erp_product_info($receivingList);

        //获取可选库位
        $whs_code_s = array_unique(array_column($data['receivingList'], "whs_code"));
                $grid_data=array();
        if($whs_code_s){
        $grid_list = $this->grid_service->get_grid_by_whscode($whs_code_s);
        foreach ($grid_list as $key => $value)
        {
            if (empty($grid_data[$value['whs_code']]))
            {
                $grid_data[$value['whs_code']][] = $value;
            }
            else
            {
                array_push($grid_data[$value['whs_code']], $value);
            }
        }
        }
        $data['grid_data']=$grid_data;
        $this->load->view('defaults/wms/receiving_order_list', $data);
        $this->load->view('defaults/common/footer', $data);
    }
    function select_grid()
    {
         $gp = gp();
         $request=explode("_", $gp['ids']);
         $sku=$request[2];
         $grid=$gp['grid_id'];
         $whs_code=$request[1];
         $request_url=$gp['url'];
         $success=$this->grid_service->add_product_grid($sku,$grid,$whs_code);
         if($success){
                       redirect($request_url);
         }else{
            js_goto($request_url, lang('wms_tips_20016') );
         }
    }
    public function store_in($itemId=0)
    {
        if ($itemId != 0)
        {
            $urlParam = @http_build_query($this->input->get());
            $success = $this->receiving_order_service->storage_by_item_id_t(intval($itemId));
        }
        redirect(blang_url('wms/ReceivingOrderList/show_list?' . $urlParam));
    }

    public function store_in_batch()
    {
        $gp = gp();
        $ids = explode(",", $gp['ids']);
        $success = $this->receiving_order_service->storage_by_item_ids_t($ids);
        $urlParamList = $this->input->get();
        unset($urlParamList['ids']);
        $urlParam = @http_build_query($urlParamList);
        redirect(blang_url('wms/ReceivingOrderList/show_list?' . $urlParam));
    }

    function create_panel()
    {
        $this->load->view('defaults/wms/zcsq_from_sap_panel');
        $this->load->view('defaults/common/footer');
    }

    function create_panel_sap($tranOrder)
    {
        //检查是否已经建了转储订单
        $is_create = $this->receiving_order_service->get_receiving_order_list_total($whs = '', $sku = '', $tranOrder);
        if ($is_create)
        {
            js_goto(blang_url('wms/ReceivingOrderList/create_panel'), $tranOrder . lang('wms_tips_20017'));
        }
        $topics = $this->sap_service->getZCSQ($tranOrder);
        if(!$topics){
            js_goto(blang_url('wms/ReceivingOrderList/create_panel'), lang('wms_tips_zbdzcsqd'));exit;
        }
        $data['items'] = $topics;
        $data['WhsName1'] = $topics[0]['WhsName1'];
        $data['WhsName2'] = $topics[0]['WhsName2'];
        $data['U_ShipName'] = $topics[0]['U_ShipName'];
        $data['U_wlmc'] = $topics[0]['U_wlmc'];
        $data['ShipNum'] = $topics[0]['ShipNum'];
        $data['NumAtCard'] = $topics[0]['NumAtCard'];
        //如果是代管仓需要重新选择仓库
        if (strstr($data['WhsName2'], "代管仓"))
        {
            $codes = $this->warehouse_list_service->get_merchant($topics[0]['WhsCode2']);
            $data['merchant_code'] = $codes;
            $data['msg'] = lang('wms_tips_20018');
        }
        elseif (strstr($data['WhsName2'], "维保"))
        {
            $data['msg'] = lang('wms_tips_20019');
        }
        else
        {
            js_goto(blang_url('wms/ReceivingOrderList/create_panel'), lang('wms_tips_20020'));
        }
        $this->load->view('defaults/wms/zcsq_from_sap_create_panel', $data);
        $this->load->view('defaults/common/footer');
    }

}

?>
